//
//  OTAddEmployeeHeaderView.m
//  OT_OC
//
//  Created by yunmian on 2019/7/18.
//  Copyright © 2019 James. All rights reserved.
//

#import "OTAddEmployeeHeaderView.h"
#import "OTAddEmployeeModel.h"

@interface OTAddEmployeeHeaderView ()<UITextFieldDelegate>
{
    UILabel             *titleLabel;
    UITextField         *textField;
    UIView              *lineView;
    UIImageView         *arrowIV;
}
@end

@implementation OTAddEmployeeHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    titleLabel = [[UILabel alloc] init];
    [self addSubview:titleLabel];
    
    textField = [[UITextField alloc] init];
    textField.delegate = self;
    textField.textAlignment = NSTextAlignmentRight;
    [textField addTarget:self action:@selector(textViewDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:textField];
    
    lineView = [[UIView alloc] init];
    lineView.backgroundColor = ColorHexF5F5F5;
    [self addSubview:lineView];
    
    arrowIV = [[UIImageView alloc] init];
    arrowIV.image = [UIImage imageNamed:@"arrow_black"];
    arrowIV.hidden = YES;
    [self addSubview:arrowIV];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.offset(10);
    }];
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.offset(-25);
        make.height.offset(40);
        make.left.offset(50);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.bottom.offset(0);
        make.height.offset(1);
    }];
    
    [arrowIV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.offset(-15);
        make.size.mas_equalTo(CGSizeMake(7, 14));
    }];
}

- (void)configAction {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickGroup)];
    [textField addGestureRecognizer:tap];
}

#pragma mark Set
- (void)setModel:(OTAddEmployeeModel *)model {
    _model = model;
    
    titleLabel.text = model.title;
    textField.text = model.text;
    textField.placeholder = model.placeholder;
    
    arrowIV.hidden = YES;
    if (model.type == OTAddEmployeeModelTypeSelect || model.type == OTAddEmployeeModelTypeOT) {
        [self configAction];
        arrowIV.hidden = NO;
    }
    
    if (model.type == OTAddEmployeeModelTypeNumber) {
        textField.keyboardType = UIKeyboardTypeDecimalPad;
    }
    
}

#pragma mark Event
- (void)onClickGroup {
    
    if ([self.delegate respondsToSelector:@selector(addEmployeeDidSelectedGroup:)]) {
        [self.delegate addEmployeeDidSelectedGroup:self.model];
    }
}

- (void)textViewDidChange:(UITextField *)textField {
    self.model.text = textField.text;
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (self.model.type == OTAddEmployeeModelTypeSelect) {
        return NO;
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(textFieldDidChange)]) {
        [self.delegate textFieldDidChange];
    }
}
@end
